<!DOCTYPE html>
<html>
<head>
<#include "../include/include.ftl"/>
<meta charset="utf-8">
<title>缴纳单位列表</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8" />
<script>
	document.onkeydown = function(e) { // 回车提交表单
		var theEvent = window.event || e;
		var code = theEvent.keyCode || theEvent.which;
		if (code == 13) {
			$('#searchBtn').click();
		}
	}

	layui.use('table', function() {//初始化table
		var table = layui.table;
		//方法级渲染
		table.render({
			id : 'unitList',//定义tableid
			elem : '#unitList',
			url : '${basePath}/api/unit/find-all/',
			response : {
				dataName : 'result'//数据列表的字段名称，默认：data
			},
			cellMinWidth : 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
			,
			cols : [ [ {
				field : 'name',
				width : '20%',
				title : '单位名称',
				sort : true
			}, {
				field : 'type',
				width : '11%',
				title : '单位类型',
				toolbar : '#type',
				sort : true
			}, {
				field : 'corporate_account',
				width : '15%',
				title : '对公账号'
			}, {
				field : 'principal',
				title : '负责人',
				width : '11%',
				minWidth : 100
			} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
			, {
				field : 'phone_number',
				title : '联系电话',
				width : '11%',
				sort : true
			}, {
				field : 'address',
				title : '地址',
				sort : true
			}, {
				field : 'operate',
				title : '操作',
				toolbar : '#operate',
				sort : false
			} ] ],
			page : true,
			height : 'full-83'
		});

		var $ = layui.$, active = {
			select : function() {//button data-type为select点击时调用此方法
				var name = $("#name").val();
				var corporateAccount = $("#corporateAccount").val();
				table.reload('unitList', {//重载unitList表格
					where : {//定义参数
						name : name,
						corporateAccount : corporateAccount
					}
				});
			},
			reload : function() {
				$("#name").val('');
				$("#corporateAccount").val('');
				table.reload('unitList', {
					where : {
						name : null,
						corporateAccount : null
					}
				});
			},
			add : function() {
				popup('添加单位', 'unit-form', 700, 450, "unit-add");
			}
		};

		$('.layui-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

	});
	
	//操作按钮控制
	layui.use('table', function() {
		var table = layui.table;

		//监听工具条
		table.on('tool(demo)', function(obj) {
			var data = obj.data;
			var status = null;
			if (data.status == 1) {
				status = "停用";
			} else {
				status = "启用";
			}
			if (obj.event === 'detail') {
				popup('查看单位', "${basePath}/paymentUnit/unit-view?id="+data.id, 700, 450, "unit-view");
			} else if (obj.event === 'del') {
				layer.confirm("确定要" + status + " " + data.name + "?", function(
						index) {
					$.ajax({
						url : "${basePath}/api/unit/change-unit-status",
						type : 'get',
						data : data,
						traditional : true,
						success : function(d) {
							if (!(d.code % 2 == 0)) {
								layer.close(index);
								layui.table.reload("unitList");
								window.top.layer.msg(d.msg, {
									icon : 6,
									offset : 'rb',
									area : [ '200px', '80px' ],
									anim : 2
								});
							} else {
								layer.msg(d.msg, {
									icon : 5
								});
							}
						},
						error : function(e) {
							layer.alert("发生错误", {
								icon : 6
							}, function() {
								layer.close(index);
								layui.table.reload("unitList");
							});
						}
					});

					return false;
				});
			} else if (obj.event === 'edit') {
				popup('修改单位', "${basePath}/paymentUnit/unit-modify?id="+data.id, 700, 450, "unit-modify");
			}
		});
	});
</script>

<script type="text/html" id="operate">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="{{ d.status == 1? 'layui-btn layui-btn-danger layui-btn-xs' : 'layui-btn  layui-btn-warm layui-btn-xs' }}" lay-event="del">{{ d.status == 1? '停用' : '启用' }}</a>
</script>

<script type="text/html" id="type">
  {{ d.type == 1? '学校' : d.type == 2?'物业':'未设置' }}
</script>

</head>

<body>
<div class="layui-form" style="margin-top: 10px">
			单位名称:
			<div class="layui-inline">
				<input type="text" id="name" autocomplete="off" class="layui-input"
					height="20px">
			</div>
			单位类型:
			<div class="layui-inline">
					<select name="type" lay-search >
						<option>请选择</option>
						<option value="1">学校</option>
						<option value="2">物业</option>
					</select>
			</div> 
			对公账号:
			<div class="layui-inline">
				<input type="text" id="corporateAccount" autocomplete="off"
					class="layui-input" height="20px">
			</div>
			<button class="layui-btn layui-btn-sm" id="searchBtn"
				data-type="select">
				<i class="layui-icon"></i>
			</button>
			<button class="layui-btn layui-btn-sm" data-type="reload">
				<i class="layui-icon layui-icon-refresh"></i>
			</button>
	</div>
	<div class="layui-col-md12" style="height: 40px; margin-top: 3px;">
		<div class="layui-btn-group">
			<button class="layui-btn layui-btn-normal" data-type="add">
				<i class="layui-icon">&#xe608;</i>新增
			</button>
		</div>
	</div>

	<table class="layui-hide" id="unitList" lay-filter="demo"></table>
</body>
</html>